/*---------------------------------------------------------------------------
// 
//  File:  sas.h
//  Purpose:  Host Universal Architecture: sas resource
//  History:  2013-08-26 Created  
//
---------------------------------------------------------------------------*/
#ifndef _CIEHS_SAS_H
#define _CIEHS_SAS_H


#include "common.h"
#include "../common/sm_kapi.h"
#include "../cistack/xmsg.h"
#include "../../dvb/include/porting/port.h"

#ifdef __cplusplus
extern "C" {
#endif

#define CIEHS_SAS_RESOURCE_ID_V1				0x00961001

#define CIEHS_SAS_TAG_CONNECT_REQ		0x9F9A00
#define CIEHS_SAS_TAG_CONNECT_CNF		0x9F9A01
#define CIEHS_SAS_TAG_ASYNC_MSG			0x9F9A07


#define CIEHS_SAS_MAX_SESSION_NUM			10
#define CIEHS_SAS_INVALID_SESSION_INDEX		0xFFFF
#define CIEHS_SAS_INVALID_PLUGIN_INSTANCE	0xFFFFFFFF
#define CIEHS_SAS_INVALID_HANDLE			0xFFFFFFFF
#define CIEHS_SAS_MAX_APPLICATION_ID_LEN	0x8

//SAS_session_status
#define CIEHS_SAS_CONNECT_OK					0x00
#define CIEHS_SAS_NO_VENDOR_CARD_APPLICATION	0x01
#define CIEHS_SAS_NO_MORE_CONNECTION			0x02

#define CIEHS_SAS_APPLICATION_ID_LEN		0x8
#define CIEHS_SAS_INVALID_HANDLE			0xFFFFFFFF
#define CIEHS_INVALID_PLUGIN_ID				0xFFFFFFFF
#define CIEHS_INVALID_SAS_CONNECTION_ID		0xFFFFFFFF

typedef unsigned int ciehs_instid_t;// for SAS application
typedef unsigned int ciehs_hsas_t;
typedef void (*pfSasSessionConnected)(int status);

typedef enum
{
	CIEHS_APP_SESS_FREE,
	CIEHS_APP_SESS_SPARE,
	CIEHS_APP_SESS_CONNECT_REQ,
	CIEHS_APP_SESS_CONNECT_OK,
}ciehs_app_sess_status_t;

typedef struct
{
	unsigned short m_u16Ssnb;
	unsigned short m_u8Slotid;
}sas_ssnb_t;

typedef struct  
{
	ciehs_app_sess_status_t	m_eStatus;
	unsigned short				m_u16Ssnb;
	unsigned char					m_u8Slotid;
	ciehs_instid_t			m_u32Instid;//plugin instance
//	ciemn_key_t				m_u32PluginThreadMsgqueueKey;// for SAS application
	unsigned char					m_au8AppId[CIEHS_SAS_MAX_APPLICATION_ID_LEN];
	unsigned int				m_u32Commid;
}sas_app_sess_t;

typedef struct  
{
	unsigned char					m_u8Init;
	unsigned char					m_u8Run;
	unsigned char					m_u8ExitFlag;
	sas_app_sess_t	m_astAppSess[CIEHS_SAS_MAX_SESSION_NUM];
	unsigned int				m_u32SasTaskid;
	sm_sem_t				m_semSasTask;
	sm_sem_t				m_semLock;
}sas_data_t;

typedef enum
{
	CIEHS_SAS_MESSAGE_TYPE_CONNECT,
	CIEHS_SAS_MESSAGE_TYPE_DATA,
	CIEHS_SAS_MESSAGE_TYPE_CLOSE,
}sas_message_type_t;

typedef struct
{
	sas_message_type_t m_eType;
	unsigned char		m_u8Slotnb;
	unsigned short	m_u16Ssnb;
	unsigned char*	m_pu8Apdu;
	unsigned int	m_u32ApduLen;
}sas_msg_t;



typedef struct  
{
	unsigned short		m_u16Tag;
	unsigned short		m_u16Len;
	unsigned char*		m_msgBuf;
	unsigned char		m_reserved[SM_QUEUE_LEN-8];
}sas_async_message_t;


void SasConnectReq(unsigned char au8AppId[], ciehs_instid_t u32Instid);
void SasDisconnect(unsigned short u16SlotId, short u16Ssnb);
int sas_init(void);
int sas_on_message(xmsg_t* pXmsg);
int SasRegisterDvbInitDoneNotify(pfSasSessionConnected callback);


#ifdef __cplusplus
}
#endif

enum
{
	SAS_TUNER_RESET = 0x0001,
	SAS_TUNER_RESET_RSP,
	SAS_TUNER_LOCK,
	SAS_TUNER_LOCK_RSP,
	SAS_GET_TUNER_STATUS,
	SAS_TUNER_STATUS,
	SAS_PID_LIST,
	SAS_PID_LIST_RSP,
	SAS_TUNER_STANDBY,
	SAS_TUNER_STANDBY_RSP,
	SAS_TUNER_RESUME,
	SAS_TUNER_RESUME_RSP,
	SAS_UPGRADE_CAM,
	SAS_UPGRADE_CAM_RSP,
	SAS_GET_CAM_INFO = 0x0010,
	SAS_CAM_INFO_RSP = 0x0011,
	SAS_GET_DONGLE_TYPE = 0x0021,
	SAS_GET_DONGLE_TYPE_RSP = 0x0022,
	SAS_UPGRADE_DATA = 0x1000,
	SAS_UPGRADE_DATA_RSP = 0x1001,
	SAS_PRINTLOG_DATA = 0x1002,
	SAS_UPDATELOG_DATA = 0x1005,
	//SAS_MMI_DIAG_DATA = 0x1007,
	SAS_GET_DGL_HW  = 0x1007,
	SAS_DGL_HW_RSP  = 0x1008,
	SAS_ENL_CUS_MM  = 0x1009,
	SAS_CUS_MMI_RSP = 0x100A,
	SAS_SC_PLUG_NTF = 0X100C
};

#define SAS_RSP_TIMEOUT		3500 //ms

int cisas_reset_tuner(void);
int cisas_tuner_lock(dvb_tuner_t tune_params);
int cisas_get_tuner_status(dvb_tuner_status_t* tuner_status);
int cisas_pid_black_list (unsigned short *pid_buf, unsigned short buf_len, unsigned short pid_cnt);
int cisas_send_upgrade_data(const unsigned char* data_buf, unsigned short data_len);
int cisas_get_upgrade_status(unsigned char* data_buf, unsigned short *data_len);
int cisas_send_printlog_data(const unsigned char* data_buf, unsigned short data_len);
int cisas_get_cam_info(char *cam_info_buf);
int cisas_tuner_standby(void);
int cisas_tuner_resume(void);
int cisas_get_dongle_type(char* dongle_type_str);
int cisas_get_dongle_hw(char* dongle_hw_str, unsigned short *data_len);
int cisas_enable_mmi_status(char* enableflag, char* type, char* mmi_status_str);



#endif	//_CIEHS_SAS_H
